require('./resigter.less')
require('../../assets/css/index.less')

const { gete } = require('../../utils/dom');
const { addevent } = require('../../utils/dom');
const dom = require('../../utils/dom');
const { Account, pwd } = require('../../utils/validator');
const https = require('../../utils/https');
var CaptchaMini = require("captcha-mini");


dom.ready(function() {
    addevent("header", function() {
        location.href = 'login.html'
    })

    let capcha = new CaptchaMini();

    let capchaCode;

    capcha.draw(dom.gete('#captcha1'), r => {
        console.log(r, '验证码1');
        capchaCode = r.toLowerCase();
        addevent("button", function() {
            if (tel.value == '') {
                alert('亲,您的手机号有误!')
            } else if (!Account(tel.value)) {
                alert('亲,您的手机号不符合规则!')
            } else if (ver.value == '') {
                alert('亲,验证码不能为空')
            } else if (ver.value.toLowerCase() != capchaCode) {
                alert('亲,验证码有误!')

            } else if (psw.value == '') {
                alert('亲,密码不能为空')
            } else if (!pwd(psw.value)) {
                alert('亲,密码不符合规则')
            } else if (psw1.value != psw.value) {
                alert('亲,两次输入密码不一致')
            } else {

                https.post('/api/user/isExist', {
                    account: tel.value
                }, function(res) {
                    console.log(11);
                    if (res.errno == 0) {
                        https.post('/api/user/register', {
                            account: tel.value,
                            password: psw.value
                        }, function(res) {
                            console.log(res);
                            if (res.errno == 0) {
                                alert('注册成功')

                                location.href = 'login.html'

                            } else {
                                alert('注册失败')
                            }
                        })
                    } else {

                        alert('不可以使用当前账号');
                    }
                })

            }

        })



    });
})